<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="总登记号" prop="generalAccessionNumber">
        <el-input
          v-model="queryParams.generalAccessionNumber"
          placeholder="请输入总登记号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择类别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_CATEGORY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="原名" prop="originalName">
        <el-input
          v-model="queryParams.originalName"
          placeholder="请输入原名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item label="质地类别" prop="material">
        <el-cascader
          v-model="queryParams.material"
          :options="cataList"
          :props="defaultPropsTree"
          filterable
          placeholder="请输入质地类别"
          class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="实际数量" prop="actureQuantity">
        <el-input
          v-model="queryParams.actureQuantity"
          placeholder="请输入实际数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="质量" prop="quality">
        <el-input
          v-model="queryParams.quality"
          placeholder="请输入质量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="件数(套)" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入件数(套)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item label="级别" prop="relicLevel">
        <el-select
          v-model="queryParams.relicLevel"
          placeholder="请选择级别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_GRADE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="relicSource">
        <el-select
          v-model="queryParams.relicSource"
          placeholder="请选择来源"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="完残状况" prop="completenessDegree">
        <el-select
          v-model="queryParams.completenessDegree"
          placeholder="请选择完残状况"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_CONDITION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保存状态" prop="preservationState">
        <el-select
          v-model="queryParams.preservationState"
          placeholder="请选择保存状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.COLLECTION_PRESERVATION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入馆日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <!--      <el-form-item label="画心尺寸-长" prop="paintingLength">
        <el-input
          v-model="queryParams.paintingLength"
          placeholder="请输入画心尺寸-长"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="画心尺寸-宽" prop="paintingWidth">
        <el-input
          v-model="queryParams.paintingWidth"
          placeholder="请输入画心尺寸-宽"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="画心尺寸-高" prop="paintingHeight">
        <el-input
          v-model="queryParams.paintingHeight"
          placeholder="请输入画心尺寸-高"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="装裱尺寸-长" prop="mountingLength">
        <el-input
          v-model="queryParams.mountingLength"
          placeholder="请输入装裱尺寸-长"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="装裱尺寸-宽" prop="mountingWidth">
        <el-input
          v-model="queryParams.mountingWidth"
          placeholder="请输入装裱尺寸-宽"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="装裱尺寸-高" prop="mountingHeight">
        <el-input
          v-model="queryParams.mountingHeight"
          placeholder="请输入装裱尺寸-高"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内容题材" prop="subjectMatter">
        <el-input
          v-model="queryParams.subjectMatter"
          placeholder="请输入内容题材"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="技法风格" prop="techniqueStyle">
        <el-input
          v-model="queryParams.techniqueStyle"
          placeholder="请输入技法风格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="款识信息" prop="inscriptionInfo">
        <el-input
          v-model="queryParams.inscriptionInfo"
          placeholder="请输入款识信息"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="形状" prop="shape">
        <el-input
          v-model="queryParams.shape"
          placeholder="请输入形状"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="钮式" prop="knobStyle">
        <el-input
          v-model="queryParams.knobStyle"
          placeholder="请输入钮式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文字类型" prop="textType">
        <el-select
          v-model="queryParams.textType"
          placeholder="请选择文字类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="印文排列" prop="textArrangement">
        <el-input
          v-model="queryParams.textArrangement"
          placeholder="请输入印文排列"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="边框特征" prop="borderFeature">
        <el-input
          v-model="queryParams.borderFeature"
          placeholder="请输入边框特征"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="印面尺寸" prop="sealSize">
        <el-input
          v-model="queryParams.sealSize"
          placeholder="请输入印面尺寸"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<!--        <el-button @click="initAttach"><Icon icon="ep:search" class="mr-5px" /> 初始化附件</el-button>-->
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['dr:collection-management:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="warning"
          plain
          @click="handleImport"
          v-hasPermi="['dr:collection-management:export']"
        >
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['dr:collection-management:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!--      <el-table-column label="自增ID" align="center" prop="id" />-->
      <!--      <el-table-column label="封面图片" align="center" prop="coverImage" />-->
      <el-table-column label="总登记号" align="center" prop="generalAccessionNumber" />
      <el-table-column label="类别" align="center" prop="category">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COLLECTION_CATEGORY" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <!--      <el-table-column label="原名" align="center" prop="originalName" />
      <el-table-column label="附件信息" align="center" prop="attachments" />
      <el-table-column label="质地类别" align="center" prop="materialType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COLLECTION_MATERIAL_TYPE" :value="scope.row.materialType" />
        </template>
      </el-table-column>
      <el-table-column label="件数(套)" align="center" prop="numberOfCases" />
      <el-table-column label="实际数量" align="center" prop="actualQuantity" />-->
      <!--      <el-table-column label="质量" align="center" prop="quality" />-->
      <el-table-column label="级别" align="center" prop="relicLevel">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COLLECTION_GRADE" :value="scope.row.relicLevel" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="来原捐赠单位或捐赠人" align="center" prop="sourceExtention" />-->
      <el-table-column label="来源" align="center" prop="relicSource">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COLLECTION_SOURCE" :value="scope.row.relicSource" />
        </template>
      </el-table-column>
      <el-table-column label="完残程度" align="center" prop="completenessDegree">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.COLLECTION_CONDITION_STATUS"
            :value="scope.row.completenessDegree"
          />
        </template>
      </el-table-column>
      <el-table-column label="保存状态" align="center" prop="preservationState">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.COLLECTION_PRESERVATION_STATUS"
            :value="scope.row.preservationState"
          />
        </template>
      </el-table-column>
      <el-table-column label="入馆日期" align="center" prop="collectionYear" />
      <!--      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="画心尺寸-长" align="center" prop="paintingLength" />
      <el-table-column label="画心尺寸-宽" align="center" prop="paintingWidth" />
      <el-table-column label="画心尺寸-高" align="center" prop="paintingHeight" />
      <el-table-column label="装裱尺寸-长" align="center" prop="mountingLength" />
      <el-table-column label="装裱尺寸-宽" align="center" prop="mountingWidth" />
      <el-table-column label="装裱尺寸-高" align="center" prop="mountingHeight" />
      <el-table-column label="内容题材" align="center" prop="subjectMatter" />
      <el-table-column label="技法风格" align="center" prop="techniqueStyle" />
      <el-table-column label="款识信息" align="center" prop="inscriptionInfo" />
      <el-table-column label="形状" align="center" prop="shape" />
      <el-table-column label="钮式" align="center" prop="knobStyle" />
      <el-table-column label="印文内容" align="center" prop="sealContent" />
      <el-table-column label="文字类型" align="center" prop="textType" />
      <el-table-column label="印文排列" align="center" prop="textArrangement" />
      <el-table-column label="边框特征" align="center" prop="borderFeature" />
      <el-table-column label="印面尺寸" align="center" prop="sealSize" />-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="180px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openView(scope.row.id)"
            v-hasPermi="['dr:collection-management:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['dr:collection-management:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['dr:collection-management:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CollectionManagementForm ref="formRef" @success="getList" />
  <!-- 查看弹窗 -->
  <CollectionManagementView ref="viewRef" />
  <!-- 导入 -->
  <CollectionImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { defaultPropsTree, handleTree } from '@/utils/tree'
import { CollectionManagementApi, CollectionManagementVO } from '@/api/dr/collectionmanagement'
import { getDictDataByParentId } from '@/api/system/dict/dict.data'
import CollectionManagementForm from './CollectionManagementForm.vue'
import CollectionManagementView from './CollectionManagementView.vue'
import CollectionImportForm from '@/components/CollectionImportForm.vue'

/** 藏品管理 列表 */
defineOptions({ name: 'CollectionManagement' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CollectionManagementVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  generalAccessionNumber: undefined,
  numberType: undefined,
  name: undefined,
  number: undefined,
  numberOfCases: undefined,
  originalName: undefined,
  era: undefined,
  numberUnit: undefined,
  geologicalEra: undefined,
  actualQuantity: undefined,
  archaeologicalEra: undefined,
  sizeLength: undefined,
  chineseHistoricalEra: undefined,
  sizeWidth: undefined,
  sizeHeight: undefined,
  specificEra: undefined,
  specificSize: undefined,
  category: undefined,
  weightRange: undefined,
  materialCategory1: undefined,
  materialCategory2: undefined,
  specificWeight: undefined,
  weightUnit: undefined,
  material: undefined,
  relicLevel: undefined,
  relicSource: undefined,
  completenessDegree: undefined,
  completenessCondition: undefined,
  preservationState: undefined,
  collectionTimeRange: undefined,
  collectionYear: undefined,
  collectionRegistrationNumber: undefined,
  version: undefined,
  storageVolume: undefined,
  administrativeDivision: undefined,
  unitCode: undefined,
  unitName: undefined,

  //扩展信息
  paintingLength: undefined,
  paintingWidth: undefined,
  paintingHeight: undefined,
  mountingLength: undefined,
  mountingWidth: undefined,
  mountingHeight: undefined,
  subjectMatter: undefined,
  techniqueStyle: undefined,
  inscriptionInfo: undefined,
  shape: undefined,
  knobStyle: undefined,
  sealContent: undefined,
  textType: undefined,
  textArrangement: undefined,
  borderFeature: undefined,
  sealSize: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const queryData = {
      ...queryParams,
      materialCategory1: queryParams.material?.[0],
      materialCategory2: queryParams.material?.[1],
      material: queryParams.material?.[2]
    }
    const data = await CollectionManagementApi.getCollectionManagementPage(queryData)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const cataList = ref<any[]>([])
const getDictData = () => {
  getDictDataByParentId({ parentId: 1 }).then((res) => {
    console.log('getDictDataByParentId res', res)
    cataList.value = handleTree(res)
  })
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 搜索按钮操作 */
const initAttach = async () => {
  CollectionManagementApi.initAttach()
  message.success(t('common.delSuccess'))
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const viewRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
const openView = (id: number) => {
  viewRef.value.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CollectionManagementApi.deleteCollectionManagement(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CollectionManagementApi.exportCollectionManagement(queryParams)
    download.excel(data, '藏品管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  getDictData()
})
</script>
